using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbAmthucDAL
    {
        private SqlConnection cn = null;
        public tbAmthucDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbAmthuc am)
        {
            SqlCommand cm = new SqlCommand("sp_amthuc_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tenamthuc", am.Tenamthuc);
            cm.Parameters.AddWithValue("@hinhanh", am.Hinhanh);
            cm.Parameters.AddWithValue("@mota", am.Mota);
            cm.ExecuteNonQuery();
        }
        public void Update(tbAmthuc am)
        {
            SqlCommand cm = new SqlCommand("sp_amthuc_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maamthuc", am.Maamthuc);
            cm.Parameters.AddWithValue("@tenamthuc", am.Tenamthuc);
            cm.Parameters.AddWithValue("@hinhanh", am.Hinhanh);
            cm.Parameters.AddWithValue("@mota", am.Mota);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_amthuc_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maamthuc", iD);
            cm.ExecuteNonQuery();
        }
        public tbAmthucCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_amthuc_Get_2000", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbAmthucCollection  atColl = new tbAmthucCollection ();

            while (dr.Read())
            {
                tbAmthuc  athuc = new tbAmthuc ();
                athuc.Maamthuc  = int.Parse(dr.GetValue(0).ToString());
                athuc.Tenamthuc  = (dr.GetValue(1).ToString());
                athuc.Hinhanh  = dr.GetValue(2).ToString();
                athuc.Mota  = dr.GetValue(3).ToString();
               
                atColl.Add(athuc);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return atColl;
        }
    }
}
